package com.example.qxfw.rcyw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.qxfw.common.entity.yj.RadarWarnMonitorInfo;
import com.example.qxfw.rcyw.mapper.RadarWarnMonitorInfoMapper;
import com.example.qxfw.rcyw.service.RadarWarnMonitorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RadarWarnMonitorInfoServiceImpl implements RadarWarnMonitorInfoService {

    @Autowired
    private RadarWarnMonitorInfoMapper radarWarnMonitorInfoMapper;

    @Override
    public List<RadarWarnMonitorInfo> selectListByTimeAndName(Integer radHour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Timestamp endTime = Timestamp.valueOf(sdf.format(new Date()));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - radHour);
        Timestamp startTime = Timestamp.valueOf(sdf.format(calendar.getTime())) ;
        LambdaQueryWrapper<RadarWarnMonitorInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RadarWarnMonitorInfo::getIsremoved, 0).eq(RadarWarnMonitorInfo::getSystemname, "制作系统").between(RadarWarnMonitorInfo::getWarningtime, startTime, endTime).orderByDesc(RadarWarnMonitorInfo::getWarningtime);
        return radarWarnMonitorInfoMapper.selectList(wrapper);
    }

    @Override
    public Integer update(String filepaths) {
        int count = 0;
        String[] idarr = filepaths.split(",");
        for (int k = 0; k < idarr.length; k++) {
            RadarWarnMonitorInfo info = new RadarWarnMonitorInfo();
            info.setIsremoved((short)1);
            LambdaUpdateWrapper<RadarWarnMonitorInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(RadarWarnMonitorInfo::getFilepath, idarr[k]);
            count += radarWarnMonitorInfoMapper.update(info, updateWrapper);
        }
        return count;
    }
}